package cate.game.role.bag.rune;

import cate.common.table.item.rune.row.RuneBaseRow;
import cate.common.table.item.rune.row.RunePosRow;
import cate.common.util.GameResult;
import cate.game.attr.PowerAttrPart;
import cate.game.attr.RunesPowerAttrPart;
import cate.game.play.support.PlayBuildContext;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.bag.BagContainer;
import cate.game.role.bag.hero.Hero;
import cate.game.role.bag.hero.OnHeroItem;
import com.fasterxml.jackson.annotation.JsonIgnore;
import cp.solution.util.collection.MapKit;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.springframework.data.annotation.Transient;

import java.util.*;

@NoteClass(value= "徽章")
public class RunesOn extends RoleDependent implements OnHeroItem<Rune> {
    @JsonIgnore
    @Transient
    public transient Hero owner;

    @NoteField(value = "英雄身上的装备们", detail = "Map<部位:Byte, 装备信息>")
    public Map<Byte, Rune> runeMap;

    public RunesOn(){

    }

    public void initialize(Hero owner, Role role){
        super.initialize(role);
        this.owner = owner;
        if(runeMap == null){
            runeMap = new HashMap<>();
        }
        runeMap.values().forEach(a -> a.initialize(role));
    }

    @Override
    public GameResult<Void> preCheckOn(PlayBuildContext ctx, byte pos, Rune rune){
        RuneBaseRow row = rune.getTpl(ctx);
        GameResult<Void> r = new GameResult<>();
        if (row == null) {
            return r.fail("徽章配置有误");
        }
        if(row.isExpRune){
            return r.fail("经验徽章不能穿戴");
        }
        RunePosRow posRow = ctx.runeExcel().pos.getRowByPos(pos);
        if (posRow == null) {
            return r.fail("穿戴栏位有误");
        }
        if (owner.getTpl(ctx).star < posRow.star) {
            return r.fail("英雄星级不足,无法穿戴徽章");
        }
        if (owner.getEffectiveLevel() < posRow.level) {
            return r.fail("英雄等级不足,无法穿戴徽章");
        }

        return r.success();
    }

    @Override
    @JsonIgnore
    public BagContainer<Rune> getItemBag() {
        return role.getBag().rune;
    }

    @Override
    public Map<Byte, Rune> getPosItems() {
        return runeMap;
    }

    @Override
    public void doRealOn(byte pos, Rune rune) {
        // 穿上装备
        runeMap.put(pos, rune);
    }

    @Override
    public void doRealOff(byte pos, Rune rune) {
        this.runeMap.remove(pos);
    }

    @Override
    public PowerAttrPart getPowerAttrPart(PlayBuildContext ctx) {
        return new RunesPowerAttrPart(ctx, this);
    }

    @JsonIgnore
    public List<Integer> getSkills() {
        Set<Integer> skills = new HashSet<>();
        runeMap.values()
                .forEach(a -> {
                            if (a.skillIds != null && !a.skillIds.isEmpty()) {
                                skills.addAll(a.skillIds);
                            }
                        });
        return new ArrayList<>(skills);
    }

    @Override
    public RunesOn copy() {
        RunesOn cp = new RunesOn();
        cp.runeMap = MapKit.cloneDeep(this.runeMap);
        cp.initialize(this.owner, this.role);
        return cp;
    }
}
